from selenium import webdriver
from selenium.webdriver.common.by import By

# 打开游览器和百度
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')

"""
# 通过属性和标签定位元素
kw = driver.find_element(By.XPATH,'//*[@id="kw"]')
print(kw.get_attribute('outerHTML'))

kw = driver.find_element(By.XPATH,'//input[@class="s_ipt"]')
print(kw.get_attribute('outerHTML'))

kw = driver.find_element(By.XPATH,'//input[@maxlength="255"]')
print(kw.get_attribute('outerHTML'))

driver.quit()

# 通过层级定位元素
kw = driver.find_element(By.XPATH,'//from/span/input')  # 找到from标签下面的span标签下面的第一个input标签
print(kw.get_attribute('outerHTML'))

kw = driver.find_element(By.XPATH,'//from/span')
print(kw.get_attribute('outerHTML'))

driver.quit()

# 通过标签索引定位元素
ipt = driver.find_element(By.XPATH,'//form/input[2]')
print(ipt.get_attribute('outerHTML'))
driver.quit()


# 通过逻辑运算定位 and
ipt = driver.find_element(By.XPATH,'//input[@id="kw" and @name="wd"]')
print(ipt.get_attribute('outerHTML'))
driver.quit()
"""
# 模糊匹配定位元素
hao = driver.find_element(By.XPATH,'//a[contains(text(),"hao")]')
print(hao.get_attribute('outerHTML'))


driver.quit()